package com.lc.zpyh.ui.fragment;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.gson.Gson;
import com.hjq.http.EasyHttp;
import com.hjq.http.lifecycle.ApplicationLifecycle;
import com.hjq.http.listener.HttpCallback;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.hjq.toast.ToastUtils;
import com.lc.base.BaseDialog;
import com.lc.zpyh.R;
import com.lc.zpyh.adapter.BaoJievp2Adapter;
import com.lc.zpyh.adapter.ImageAdapter;
import com.lc.zpyh.adapter.TopLineAdapter;
import com.lc.zpyh.app.TitleBarFragment;
import com.lc.zpyh.bean.ResultBean;
import com.lc.zpyh.http.glide.GlideApp;
import com.lc.zpyh.http.request.OpenEmergencyCabinetryApi;
import com.lc.zpyh.http.request.PublicMsgBean;
import com.lc.zpyh.http.request.SelectBySchoolApi;
import com.lc.zpyh.http.request.SelectByShareApi;
import com.lc.zpyh.http.request.SelectCanteenBySchoolIdApi;
import com.lc.zpyh.http.request.SelectCounpByCanteenIdApi;
import com.lc.zpyh.http.request.SelectMyCartCountApi;
import com.lc.zpyh.http.request.SelectMyMessageNoCountApi;
import com.lc.zpyh.http.request.SelectNewsBySchoolApi;
import com.lc.zpyh.http.request.SelectOneLVApi;
import com.lc.zpyh.http.request.SelectRecommendMerchantsApi;
import com.lc.zpyh.http.response.BannerBean;
import com.lc.zpyh.http.response.BannerHotBean;
import com.lc.zpyh.http.response.SelectByShareBean;
import com.lc.zpyh.http.response.SelectCanteenBySchoolBean;
import com.lc.zpyh.http.response.SelectCounpByCanteenIdBean;
import com.lc.zpyh.http.response.SelectMyCartCountBean;
import com.lc.zpyh.http.response.SelectMyMessageNoCountBean;
import com.lc.zpyh.http.response.SelectOneLVBean;
import com.lc.zpyh.http.response.SelectRecommendMerchantsBean;
import com.lc.zpyh.other.IntentKey;
import com.lc.zpyh.ui.activity.MainActivity;
import com.lc.zpyh.ui.activity.ShopCarActivity;
import com.lc.zpyh.ui.activity.home.AddressSearchActivity;
import com.lc.zpyh.ui.activity.home.FoodNewsActivity;
import com.lc.zpyh.ui.activity.home.MessageDeatailsActivity;
import com.lc.zpyh.ui.activity.home.SearchActivity;
import com.lc.zpyh.ui.activity.mine.LoginCodeActivity;
import com.lc.zpyh.ui.activity.mine.RechargeActivity;
import com.lc.zpyh.ui.activity.order.OrderTakeMealActivity;
import com.lc.zpyh.ui.activity.supermarket.BusinessActivity;
import com.lc.zpyh.ui.dialog.WaitDialog;
import com.lc.zpyh.util.BigDecimalUtils;
import com.lc.zpyh.util.BtnUtils;
import com.lc.zpyh.util.CourseUtils;
import com.lc.zpyh.util.SPUtil;
import com.lc.zpyh.util.constant.EvenConstant;
import com.lc.zpyh.util.eventbus.Event;
import com.lc.zpyh.view.HomeShareDialog;
import com.lc.zpyh.view.MyViewPagerIndicator;
import com.lc.zpyh.widget.CollectCouponsDialog;
import com.lc.zpyh.widget.UIHelper;
import com.lc.zpyh.zxing.activity.MipcaActivityCapture;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.youth.banner.Banner;
import com.youth.banner.config.IndicatorConfig;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.transformer.ZoomOutPageTransformer;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;
import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

public class HomeFoodFragment extends TitleBarFragment<MainActivity> {
    private static final String TAG = "HomeFoodFragment";
    @BindView(R.id.vp)
    ViewPager2 vp;
    @BindView(R.id.indicator_two)
    MyViewPagerIndicator indicatorTwo;
    @BindView(R.id.tv_home_address)
    TextView tvHomeAddress;
    @BindView(R.id.tv_news_num)
    TextView tvNewsNum;
    @BindView(R.id.rl_shopping_news)
    RelativeLayout rlShoppingCart;
    @BindView(R.id.rl_top)
    RelativeLayout rlTop;
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.banner_hot)
    Banner bannerHot;
    @BindView(R.id.rv_home_goods)
    RecyclerView rvHomeGoods;
    @BindView(R.id.nestedscrollview)
    NestedScrollView nestedscrollview;
    @BindView(R.id.layout_refresh)
    TwinklingRefreshLayout refreshLayout;
    @BindView(R.id.ll_inTheEnd)
    LinearLayout llInTheEnd;
    @BindView(R.id.car_iv)
    ImageView carIv;
    @BindView(R.id.tv_cart_num)
    TextView tvCartNnum;
    private BaseQuickAdapter<SelectRecommendMerchantsBean.ListBean.DataBean, BaseViewHolder> adapter;
    private CollectCouponsDialog collectCouponsDialog;
    private String schoolId;
    private BaseDialog mWaitDialog;
    private List<SelectRecommendMerchantsBean.ListBean.DataBean> dataBeanList =new ArrayList<>();
    private int page = 1;
    private int total;
    private BaoJievp2Adapter baoJievp2Adapter;
    private final int SCANNIN_GREQUEST_CODE = 107;//扫描二维码
    private HomeShareDialog homeShareDialog;
    private int newscount;
     private List<BannerBean.ListBean> imgDataList=new ArrayList<>();
    private ImageAdapter imageAdapter;
    private List<SelectOneLVBean.MerchantClassifiesBean> oneLvList=new ArrayList<>();
    private List<SelectCanteenBySchoolBean.ListBean> dataSchoolList;
    private String userid;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_homefood;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (mWaitDialog == null) {
            mWaitDialog = new WaitDialog.Builder(getContext()).create();
                    // 消息文本可以不用填写
        }
//        initbanner();
//        initbannerhot();

        if (!SPUtil.get(getContext(), IntentKey.ADDRESSID,"").isEmpty()&&!SPUtil.get(getContext(), IntentKey.ADDRESS,"").isEmpty()) {
            tvHomeAddress.setText(SPUtil.get(getContext(), IntentKey.ADDRESS,""));

            schoolId = SPUtil.get(getContext(), IntentKey.ADDRESSID,"");
            Log.e(TAG, "onViewCreated: "+schoolId);


            initbannerhot();
//            banner
            initbanner();
//          根据学下查询一级分类
            selectCanteenBySchoolId();

            setUpAdapter(page+"");
        }



        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                page = 1;
                setUpAdapter(page+"");
                initbanner();
                refreshLayout.finishRefreshing();

            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                refreshLayout.finishLoadmore();
//                if (!TextUtils.isEmpty(goods_classify_id)) {



                if (dataBeanList.size() < total) {
                    page++;
                      setUpAdapter(page+"");
                } else {
                    refreshLayout.setEnableLoadmore(false);
                    llInTheEnd.setVisibility(View.VISIBLE);
                }
//         }
            }
        });

/*
首页弹窗优惠券
 */
//        collectCouponsDialog = new CollectCouponsDialog(getContext(), R.layout.dialog_collectcoupons,"");
//
//        collectCouponsDialog.show();
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!SPUtil.get(getContext(), IntentKey.ADDRESSID, "").isEmpty() && !SPUtil.get(getContext(), IntentKey.USERID, "").isEmpty()) {
            selectMyCartCount();
        }

        selectMessages();

    }
    private void selectMyCartCount() {
        if (SPUtil.get(getContext(), IntentKey.USERID, "") == null || SPUtil.get(getContext(), IntentKey.USERID, "").isEmpty()) {
            userid = "";
        } else {
            userid = SPUtil.get(getContext(), IntentKey.USERID, "");
        }
        EasyHttp.post(this)
                .api(new SelectMyCartCountApi().setSchoolId(SPUtil.get(getContext(), IntentKey.ADDRESSID, "")).setUserId(userid)).request(new HttpCallback<SelectMyCartCountBean>(this) {

            @Override
            public void onSucceed(SelectMyCartCountBean data) {
                if (data.getCode() == 0) {
                    SPUtil.put(getContext(), IntentKey.CAR_COUNT, data.getList() + "");

                    if (SPUtil.get(getContext(), IntentKey.CAR_COUNT, "").isEmpty()||SPUtil.get(getContext(), IntentKey.CAR_COUNT, "")==null||SPUtil.get(getContext(), IntentKey.CAR_COUNT, "").equals("0")){
                        tvCartNnum.setVisibility(View.GONE);
                    }else {
                        tvCartNnum.setVisibility(View.VISIBLE);
                        tvCartNnum.setText(SPUtil.get(getContext(), IntentKey.CAR_COUNT, ""));
                    }
                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }
    private void selectMessages() {
        if(SPUtil.get(getContext(), IntentKey.USERID, "").equals("")){
            tvNewsNum.setVisibility(View.GONE);
            return;
        }

        EasyHttp.post(this)
                .api(new SelectMyMessageNoCountApi().setUserid(SPUtil.get(getContext(), IntentKey.USERID, ""))).request(new HttpCallback<SelectMyMessageNoCountBean>(this) {

            @Override
            public void onSucceed(SelectMyMessageNoCountBean data) {
                if (data.getCode()==0) {
                    if (data.getCount() == 0) {

                        tvNewsNum.setVisibility(View.GONE);
                    } else {
                        tvNewsNum.setVisibility(View.VISIBLE);

                        newscount = data.getCount();
                        tvNewsNum.setText(newscount + "");
                    }
                }
            }
            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    private void initbannerhot() {
        EasyHttp.post(this)
                .api(new SelectNewsBySchoolApi().setSchoolId(schoolId)).request(new HttpCallback<BannerHotBean>(this) {

            @Override
            public void onSucceed(BannerHotBean datas) {
                if (datas.getCode()==0){
                    if (datas.getNews().size()==0){
                        datas.getNews().add(new BannerHotBean.NewsBean("1","欢迎使用悟小小","欢迎使用悟小小"));
                        datas.getNews().add(new BannerHotBean.NewsBean("1","欢迎使用悟小小","欢迎使用悟小小"));
                    }
                    bannerHot.setAdapter(new TopLineAdapter(datas.getNews()))
                            .setOrientation(Banner.VERTICAL)
                            .setPageTransformer(new ZoomOutPageTransformer())
                            .setOnBannerListener((data, position) -> {
                                startActivity(new Intent(getContext(), MessageDeatailsActivity.class).putExtra("id",datas.getNews().get(position).getNewsId()));

                     });
                }
            }
            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }
    private void initbanner() {
            EasyHttp.post(this)
                    .api(new SelectBySchoolApi().setSchoolId(schoolId)).request(new HttpCallback<BannerBean>(this) {

                @Override
                public void onSucceed(BannerBean data) {
              banner.setAdapter(new ImageAdapter(getContext(),data.getList())).setIndicator(new CircleIndicator(getContext())).setLoopTime(5000).setIndicatorGravity(IndicatorConfig.Direction.CENTER).setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void OnBannerClick(Object datas, int position) {
                        if (data.getList().get(position).getAdverState()==1){
                            if (CourseUtils.isNotNullOrEmpty(data.getList().get(position).getAdverLink())) {
                                startActivity(new Intent(getContext(), RechargeActivity.class).putExtra("title", "详情").putExtra("Url", data.getList().get(position).getAdverLink()));
                            }

                        }else {
                            if (data.getList().get(position).getMerchantId()!=null) {
                                startActivity(new Intent(getContext(), BusinessActivity.class).putExtra("id", data.getList().get(position).getMerchantId() + ""));

                            }
                        }

                    }
                });
//                    imgDataList.clear();
//                    imgDataList.addAll(data.getList());
//                    imageAdapter = new ImageAdapter(getContext(),imgDataList);
//                    banner.setAdapter(imageAdapter).setIndicator(new CircleIndicator(getContext())).setIndicatorGravity(IndicatorConfig.Direction.CENTER);
//                        imageAdapter.updateData(imgDataList);
//                        imageAdapter.notifyDataSetChanged();

//                    imageAdapter.setOnBannerListener(new OnBannerListener<BannerBean.ListBean>() {
//                        @Override
//                        public void OnBannerClick(BannerBean.ListBean data, int position) {
//                            if (imgDataList.get(position).getAdverState()==1){
//                                startActivity(new Intent(getContext(),RechargeActivity.class).putExtra("title","详情").putExtra("Url",imgDataList.get(position).getAdverLink()));
//
//                            }else {
//                                if (imgDataList.get(position).getMerchantId()!=null) {
//                                    startActivity(new Intent(getContext(), BusinessActivity.class).putExtra("id", imgDataList.get(position).getMerchantId() + ""));
//
//                                }
//                            }

//                        }
//                    });
}



                @Override
                public void onFail(Exception e) {
                    super.onFail(e);

                }
            });
    }
//    首页商家
    private void setUpAdapter(String page) {
        EasyHttp.post(this)
                .api(new SelectRecommendMerchantsApi().setSchoolId(schoolId).setPageNum(page).setPageSize("10")).request(new HttpCallback<SelectRecommendMerchantsBean>(this) {
            @Override
            public void onStart(Call call) {
                super.onStart(call);
                mWaitDialog.show();
            }

            @Override
            public void onEnd(Call call) {
                super.onEnd(call);
                mWaitDialog.dismiss();
            }

            @Override
            public void onSucceed(SelectRecommendMerchantsBean data) {
                  if (data.getCode()==0){
                      total=data.getList().getCount();
                        if (adapter==null) {
                            dataBeanList.clear();

                            dataBeanList.addAll(data.getList().getData());
                            adapter = new BaseQuickAdapter<SelectRecommendMerchantsBean.ListBean.DataBean, BaseViewHolder>
                                    (R.layout.item_shop_goods, dataBeanList) {

                                @Override
                                protected void convert(BaseViewHolder baseViewHolder, SelectRecommendMerchantsBean.ListBean.DataBean dataBean) {
                                    if (dataBean.getMerchantCoupons() != null ) {if (dataBean.getMerchantCoupons().size()==0){
                                        baseViewHolder.setVisible(R.id.tv_tag_type1, false);
                                        baseViewHolder.setVisible(R.id.tv_tag_type2, false);
                                    } else if (dataBean.getMerchantCoupons().size() == 1) {

                                            baseViewHolder.setText(R.id.tv_tag_type1, "满"+BigDecimalUtils.div(dataBean.getMerchantCoupons().get(0).getMeetTheConditions()+"","100",0)+"减"+BigDecimalUtils.div(dataBean.getMerchantCoupons().get(0).getCreditFor()+"","100",0));
                                            baseViewHolder.setVisible(R.id.tv_tag_type1, true);
                                            baseViewHolder.setVisible(R.id.tv_tag_type2, false);
                                        } else if (dataBean.getMerchantCoupons().size() >= 2) {
                                            baseViewHolder.setText(R.id.tv_tag_type1, "满"+BigDecimalUtils.div(dataBean.getMerchantCoupons().get(0).getMeetTheConditions()+"","100",0)+"减"+BigDecimalUtils.div(dataBean.getMerchantCoupons().get(0).getCreditFor()+"","100",0));
                                            baseViewHolder.setText(R.id.tv_tag_type2, "满"+BigDecimalUtils.div(dataBean.getMerchantCoupons().get(1).getMeetTheConditions()+"","100",0)+"减"+BigDecimalUtils.div(dataBean.getMerchantCoupons().get(1).getCreditFor()+"","100",0));
                                            baseViewHolder.setVisible(R.id.tv_tag_type1, true);
                                            baseViewHolder.setVisible(R.id.tv_tag_type2, true);
                                        }
                                    }
                                    baseViewHolder.setText(R.id.tv_shop_name, dataBean.getName());

                                    if (dataBean.getImg() != null) {
                                        if (dataBean.getImg().contains("http")){
                                            GlideApp.with(requireContext()).load(dataBean.getImg()).apply(new RequestOptions().placeholder(R.mipmap.zhanweiicon01).error(R.mipmap.zhanweiicon01)).into((ImageView) baseViewHolder.getView(R.id.img_shop));
                                        }else {
                                            GlideApp.with(requireContext()).load(IntentKey.ORDER_IMAGE + dataBean.getImg()).apply(new RequestOptions().placeholder(R.mipmap.zhanweiicon01).error(R.mipmap.zhanweiicon01)).into((ImageView) baseViewHolder.getView(R.id.img_shop));
                                        }

                                    }
                                    baseViewHolder.setText(R.id.tv_xiaoliang, "月售"+dataBean.getSales());
                                    if (dataBean.getStatus()==0){
                                        baseViewHolder.setGone(R.id.tv_status,true);
                                    }else {
                                        baseViewHolder.setVisible(R.id.tv_status,true);
                                    }
                                }
                            };
                            rvHomeGoods.setAdapter(adapter);
                            adapter.setOnItemClickListener(new OnItemClickListener() {
                                @Override
                                public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {

                                    if (!CourseUtils.isNotNullOrEmpty(SPUtil.get(getContext(),IntentKey.USERID,""))){
                                        startActivity(LoginCodeActivity.class);
                                        return;
                                    }
                                    startActivity(new Intent(getContext(), BusinessActivity.class).putExtra("id",dataBeanList.get(position).getId()+""));
                                }
                            });
                        }else {
                            if (!page.equals("1")) {
                                dataBeanList.addAll(data.getList().getData());
                                adapter.notifyDataSetChanged();
                            }else {
                                dataBeanList.clear();
                                dataBeanList.addAll(data.getList().getData());
                                adapter.notifyDataSetChanged();
                                llInTheEnd.setVisibility(View.GONE);
                            }
                        }

                      if (dataBeanList.size()>0) {
                          refreshLayout.setEnableLoadmore(true);
                          llInTheEnd.setVisibility(View.GONE);
                      } else {
                          refreshLayout.setEnableLoadmore(false);
                          llInTheEnd.setVisibility(View.VISIBLE);
                      }
                  }


            }
            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });






    }


    @OnClick({R.id.tv_home_address, R.id.ll_seach, R.id.icon_erweima, R.id.rl_shopping_news,R.id.car_iv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_home_address:
                UIHelper.startActivity(getContext(), AddressSearchActivity.class);
                break;
            case R.id.ll_seach:
                UIHelper.startActivity(getContext(), SearchActivity.class);
                break;
            case R.id.icon_erweima:
                if (BtnUtils.isFastClick()) {
                    if (!TextUtils.isEmpty(SPUtil.get(getContext(), IntentKey.USERID, ""))) {
                        XXPermissions.with(this)
                                .permission(Permission.CAMERA)
                                .request(new OnPermissionCallback() {

                                    @Override
                                    public void onGranted(List<String> permissions, boolean all) {
                                        if (all) {
                                            Intent intent4 = new Intent();
                                            intent4.setClass(getActivity(), MipcaActivityCapture.class);

                                            startActivityForResult(intent4, SCANNIN_GREQUEST_CODE);
                                        } else {

                                        }
                                    }
                                });

                    } else {
                        toast("请先进行用户登录!");

                    }
                }
                break;
            case R.id.rl_shopping_news:
                    UIHelper.startActivity(getContext(), FoodNewsActivity.class);
                break;
            case R.id.car_iv:

                if (!CourseUtils.isNotNullOrEmpty(SPUtil.get(getContext(),IntentKey.USERID,""))){
                    startActivity(LoginCodeActivity.class);
                    return;
                }
                UIHelper.startActivity(getContext(), ShopCarActivity.class);
                break;
        }
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);  //这个super可不能落下，否则可能回调不了

        switch (SCANNIN_GREQUEST_CODE) {
            case SCANNIN_GREQUEST_CODE:
                if (resultCode == getActivity().RESULT_OK) {
                    String codeString = data.getStringExtra("result");
                    Log.e(TAG, "onActivityResult: "+codeString );
                    try {


                    Gson gson = new Gson();
                   ResultBean resultbean = gson.fromJson(codeString, ResultBean.class);
                     if(resultbean.getQrCodeType()==4){
                         openEmergencyCabinetry(resultbean.getCabinetryId()+"");
                         return;
                     }
                    startActivity(new Intent(getContext(), OrderTakeMealActivity.class).putExtra("cabinetryId",resultbean.getCabinetryId()+"").putExtra("qrCodeType",resultbean.getQrCodeType()+""));
                    }catch (Exception e){
                        ToastUtils.show("无法识别二维码");
                    }finally {

                    }

                }
                break;
        }
    }

    private void openEmergencyCabinetry(String cabinetryId) {
        EasyHttp.post(this)
                .api(new OpenEmergencyCabinetryApi().setUserId(SPUtil.get(getContext(), IntentKey.USERID,"")).setCabinetryId(cabinetryId)).request(new HttpCallback<PublicMsgBean>(this) {

            @Override
            public void onSucceed(PublicMsgBean data) {
                    toast(data.getMsg());
            }

        });
    }


    private void selectByShare(String share) {

        EasyHttp.post(this)
                .api(new SelectByShareApi().setShare(share)).request(new HttpCallback<SelectByShareBean>(this) {

            @Override
            public void onSucceed(SelectByShareBean data) {
                if (data.getCode()==0){
                    if (data.getList().getLogo()!=null) {
                        homeShareDialog = new HomeShareDialog(getContext(), data.getList());
                        homeShareDialog.show();
                        clearClipboard();
                    }
                }

            }
            @Override
            public void onFail(Exception e) {


            }
        });
    }
    public void clearClipboard() {
        ClipboardManager manager;
        manager = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
        if (manager != null) {
            try {
                manager.setPrimaryClip(manager.getPrimaryClip());
                manager.setText(null);
            } catch (Exception e) {

            }
        }
    }
    private void selectCanteenBySchoolId() {
        EasyHttp.post(this)
                .api(new SelectCanteenBySchoolIdApi().setSchoolid(schoolId)).request(new HttpCallback<SelectCanteenBySchoolBean>(this) {

            @Override
            public void onSucceed(SelectCanteenBySchoolBean data) {
                if (data.getCode()==0) {
                    oneLvList.clear();
                    dataSchoolList = data.getList();
                    if (data.getList().size()>0){
                        for (int i = 0; i <data.getList().size() ; i++) {
                            SelectOneLVBean.MerchantClassifiesBean bean = new SelectOneLVBean.MerchantClassifiesBean();
                            bean.setClassifyName(data.getList().get(i).getName());
                            bean.setMerchantClassifyId(0);
                            bean.setCanteenId(data.getList().get(i).getId());
                            bean.setSchoolId(Integer.valueOf(schoolId));
                             bean.setImg(data.getList().get(i).getCanteenImg());
                            oneLvList.add(bean);
                        }


                    }
                    selectOneLV();
                }
                }
                @Override
                public void onFail(Exception e) {

                }
            });
        }

    private void coupon(String s,List<SelectCanteenBySchoolBean.ListBean> list) {
        EasyHttp.post(new ApplicationLifecycle())
                .api(new SelectCounpByCanteenIdApi().setId(s).setPageNum("1").setPageSize("999").setType("1").setUserId(SPUtil.get(getContext(),IntentKey.USERID,""))).request(new HttpCallback<SelectCounpByCanteenIdBean>(this) {

            @Override
            public void onSucceed(SelectCounpByCanteenIdBean data) {
                if (data.getCode()==0) {
                    if (data.getCoupons()!=null) {

                    }
                }
            }

            @Override
            public void onFail(Exception e) {


            }
        });
    }
    private void selectOneLV() {

        EasyHttp.post(this)
                .api(new SelectOneLVApi().setSchoolid(schoolId)).request(new HttpCallback<SelectOneLVBean>(this) {

            @Override
            public void onSucceed(SelectOneLVBean data) {
if (data.getCode()==0) {
    for (int i = 0; i < data.getMerchantClassifies().size(); i++) {
        SelectOneLVBean.MerchantClassifiesBean bean = new SelectOneLVBean.MerchantClassifiesBean();
        bean.setClassifyName(data.getMerchantClassifies().get(i).getClassifyName());
            bean.setCanteenId(0);
        bean.setSchoolId(data.getMerchantClassifies().get(i).getSchoolId());
        bean.setMerchantClassifyId(data.getMerchantClassifies().get(i).getMerchantClassifyId());
        bean.setImg(data.getMerchantClassifies().get(i).getImg());
        bean.setClassifyPid(data.getMerchantClassifies().get(i).getClassifyPid());
        oneLvList.add(bean);
    }

    for (int i = 0; i < 2; i++) {
        SelectOneLVBean.MerchantClassifiesBean bean = new SelectOneLVBean.MerchantClassifiesBean();
        if (i == 0) {
            bean.setCanteenId(1);
            bean.setClassifyName("邀请好友");
            oneLvList.add(bean);
        } else {
            bean.setCanteenId(1);
            bean.setClassifyName("优惠券");
            oneLvList.add(bean);
        }

    }


            baoJievp2Adapter = new BaoJievp2Adapter(getContext());

              baoJievp2Adapter.setDataList(oneLvList);
            vp.setAdapter(baoJievp2Adapter);
    Log.e(TAG, "onSucceed: "+(int) Math.ceil(Double.parseDouble(BigDecimalUtils.div( oneLvList.size()+"",8+"",2)+"")));
    int countpage=(int) Math.ceil(Double.parseDouble(BigDecimalUtils.div( oneLvList.size()+"",8+"",2)+""));

    if (countpage<2){
        indicatorTwo.setViewPager(vp,   0, false);
        indicatorTwo.setVisibility(View.GONE);
    }else {
        indicatorTwo.setVisibility(View.VISIBLE);
        indicatorTwo.setViewPager(vp,   countpage, false);
    }


}
            }
            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });

    }
    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Override
    protected void receiveEvent(Event event) {
        super.receiveEvent(event);
        if(event.getCode()== EvenConstant.QUWANSHANXIN){
            tvHomeAddress.setText(event.getData()+"");
        }else if (event.getCode()== EvenConstant.QUWANSHANXINID){
            schoolId = (String) event.getData();
            Log.e(TAG, "receiveEvent: "+schoolId);
            initbannerhot();
//            banner
            initbanner();
//          根据学下查询一级分类
            selectCanteenBySchoolId();

//            selectCanteenBySchoolId();
            setUpAdapter(1+"");
        }else if (event.getCode()== EvenConstant.CLIPBOARD){
            selectByShare((String)event.getData());
        }
        else if (event.getCode()== EvenConstant.COUPON){
            if (dataSchoolList!=null&&dataSchoolList.size()>0){
                collectCouponsDialog = new CollectCouponsDialog(getContext(),dataSchoolList);
                collectCouponsDialog.setOnGoodsOrderListener(new CollectCouponsDialog.OnGoodsOrderListener() {
                    @Override
                    public void onDismiss(String time) {

                    }

                    @Override
                    public void onBuyNow(String time) {

                    }
                });
                collectCouponsDialog.show();
            }

        }
    }

}
